package org.grow.present.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.grow.present.entity.UserInfo;
import org.grow.present.service.FileTransfer;
import org.grow.present.service.UserInfoService;
import org.grow.present.mapper.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @author xieweig
 * @description 针对表【UserInfo】的数据库操作Service实现
 * @createDate 2024-06-04 20:24:16
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
        implements UserInfoService {

    @Autowired
    private FileTransfer fileTransfer;
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public UserInfo patchModify(UserInfo userInfo, UserInfo db) throws IOException {
        String UID = db.getUserId();
        db.setNickName(userInfo.getNickName());
//        todo: transaction
        if (StringUtils.hasText(db.getPic())) {
//            说明曾经传入了头像
            if (!userInfo.getPic().equals(db.getPic())) {
//                如果曾经的头像与当下的头像不相等，则更新
                String newPic = fileTransfer.transTo(userInfo.getPic(), UID, "UserInfo");
                log.warn(db.getPic() + " -> " + newPic);
            } else {
                log.warn(db.getPic() + " -> keep pic un change ");
            }
        } else {
            //            说明从来没有传入头像
            if (StringUtils.hasText(userInfo.getPic())) {
//                说明是第一次要传入头像
                String newPic = fileTransfer.transTo(userInfo.getPic(), UID, "UserInfo");
                log.warn(db.getPic() + " -> " + newPic);
                db.setPic(newPic);
            } else {
//                说明保持头像依旧不填入 因此不必移动
                log.warn(db.getUserId() + " -> keep pic null ");
            }


        }
//        if (StringUtils.hasText(userInfo.getPic())) {
//            if (!userInfo.getPic().equals(db.getPic())) {
//                String newPic = fileTransfer.transTo(userInfo.getPic(), UID, "UserInfo");
//                log.trace(db.getPic() + " -> " + newPic);
//            }
//        }
        db.setUpdateTime(LocalDateTime.now());
        userInfoMapper.updateById(db);


        return db;
    }
}




